$(function() {
	var loading = weui.loading('loading', {
		className: 'custom-classname',
	});
	var id = Common.fn.getUrlParam('id'),
		result = Model.gift.detail({ giftId: id });
	if (result.code) {
		var href = 'index.html';
		Common.fn.formatOutput(result, href);
		loading.hide();
		return false;
	}

    // 显示商品图片
    var imageObjs = [],
		urls = [],
		clientWidth = document.body.clientWidth * 0.3;
    for (var i = 0; i < result.data[0].gift_img.length; i++) {
		var item = '<img class="goods-images-item" data-id="' + i + '" src="' + result.data[0].gift_img[i].image + '">'
		$('.goods-detail-thumb-item')
			.eq(i)
			.append(item)
		urls.push(result.data[0].gift_img[i].image)
		// 获取图片高度
        var thisWidth = parseInt($('.goods-detail-thumb-item img').eq(i).css('width')),
            thisHeight = parseInt($('.goods-detail-thumb-item img').eq(i).css('height')),
            present = (clientWidth / thisWidth) * 1.25;
        $('.goods-detail-thumb-item img').eq(i).css('height', thisHeight * present + 'px');
        $('.goods-detail-thumb-item img').eq(i).css('width', thisWidth * present + 'px');
	}
    for(var i = 0;i < urls.length;i++) {
        imageObjs['imageObj_' + i] = { urls: urls, current: urls[i] }
    }
    // 图片插件绑定
    $(document).on('click', '.goods-images-item', function() {
        var id = $(this).attr('data-id');
        previewImage.start(imageObjs['imageObj_' + id])
	});
    // 关注按钮改变
    $('.attention-btn').attr("data-id", result.data[0].business.businessId);
	if (result.data[0].attention == true) {
		// 已关注
		$('.attention-btn').text('已关注');
		$('.attention-btn').attr('data-state', 1);
	} else {
		$('.attention-btn').text('关注');
		$('.attention-btn').attr('data-state', 0);
    }
	// 设置其他信息
	$('.portrait-img').attr('src', result.data[0].business.photo);
    $('.inte-detail-nickname').text(result.data[0].business.name);
    $('.inte-detail-talk').attr('href', '../views/chat.html?cid=' + result.data[0].business.businessId + '&nickname=' + result.data[0].business.name + '#bottom');
    $('.gift-name').text(result.data[0].gift.name);
    $('.gift-number').text(result.data[0].gift.number);
    $('.gift-free').text(result.data[0].gift.free);
    $('.goods-detail').text(result.data[0].gift.content);
    $('.inte-detail-price').text(result.data[0].gift.price + '红米');
    var info = result.data[0].gift.content,
		img = result.data[0].gift_img[0].image;
	Model.wechat.giftWeShare({
        gid: id,
        img: img,
        info: info,
        isIndex: false
    });
	loading.hide();
});

// 关注按钮
$(document).on('click', '.attention-btn', function() {
	var id = $(this).attr('data-id'),
		state = $(this).attr('data-state');
	if (state == 1) {
		// 已关注
		weui.confirm(
			'真的要取消关注吗？',
			function() {
				// 取消关注
				result = Model.business.attendBusiness({
					businessId: id,
				});
				if (result.code) {
					alert(result.message);
					location.reload();
					return false;
				}
				location.reload();
			},
			function() {}
		);
	} else {
		// 关注店铺
		var loading = weui.loading('loading', {
			className: 'custom-classname',
		});
		result = Model.business.attendBusiness({
			businessId: id,
		});
		if (result.code) {
			Common.fn.formatOutput(result);
			return false;
		}
		loading.hide();
		location.reload();
	}
});

// 分享
$(document).on('click', '.share', function() {
	weui.alert('请点击右上角菜单 -> 发送给朋友或分享到朋友圈 进行分享');
});

// 点击图片放大
/* $(document).on('click', '.goods-images-item', function() {
	var url = $(this).attr('src');
	var gallery = weui.gallery(url, {
		className: 'custom-classname',
		onDelete: function() {
			gallery.hide(function() {
				console.log('`gallery` has been hidden');
			});
		},
	});
}); */

// 图片懒加载
function lazyImg() {
	$('.lazy-img').lazyload({
		effect: 'fadeIn',
	});
}

// 立即兑换
$(document).on('click', '.exchange', function() {
    $('.protocol-shade').show();
    $('.intre-detail-protocol').show();
});

// 协议按钮
$(document).on('click', '.agreement-btn', function () {
    $('.protocol-detail').css('display', 'block');
    $('.xieyi-content').css('display', 'block');
    $('.xieyi-content').html(Common.pa._MESSAGE);
});

// 关闭协议按钮
$(document).on('click', '.xieyi-close-btn', function () {
    $('.protocol-detail').css('display', 'none');
    $('.xieyi-content').css('display', 'none');
});

// 取消购买
$(document).on('click', '.ad-cancel-btn', function() {
    $('.intre-detail-protocol').hide();
    $('.protocol-shade').hide();
});

// 确定购买
$(document).on('click', '.ad-submit-btn', function() {
    var loading = weui.loading('loading', { className: 'custom-classname' });
    var id = Common.fn.getUrlParam('id'),
        result = Model.gift.exchange({ giftId: id });
    if (result.code) {
        Common.fn.formatOutput(result);
        loading.hide();
        return false;
    }
    loading.hide();
    weui.alert('礼品兑换成功！', function() {
        location.reload();
    });
});